from pathlib import Path
from qfluentwidgets import (SettingCardGroup, PushSettingCard, ScrollArea, ExpandLayout)
from qfluentwidgets import FluentIcon as FIF
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QFileDialog

from ..common.style_sheet import StyleSheet
from ..common.config import cfg
from ..utils.get_information import GetInformation
from .info_view import InformationView


class FileInterface(ScrollArea):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.scrollWidget = QWidget()
        self.expandLayout = ExpandLayout(self.scrollWidget)
        self.stateTooltip = None

        self.filePathGroup = SettingCardGroup("文件路径", self.scrollWidget)
        self.sourceFolderCard = PushSettingCard(
            "选择文件夹",
            FIF.FOLDER, 
            "源文件夹", 
            cfg.get(cfg.sourceFolder),
            parent=self.filePathGroup
        )
        self.targetFolderCard = PushSettingCard(
            "选择文件夹",
            FIF.FOLDER, 
            "目标文件夹", 
            cfg.get(cfg.targetFolder),
            parent=self.filePathGroup
        )

        self.fileTidingGroup = SettingCardGroup("文件处理", self.scrollWidget)
        self.fileTidingCard = PushSettingCard(
            "执行操作", 
            FIF.ZOOM, 
            "读取并解析文件",
            None, 
            parent=self.fileTidingGroup
        )

        self.info_view = InformationView(self.scrollWidget)

        self.__initWidget()

    def __initWidget(self):
        self.resize(1000, 870)
        self.setMinimumSize(600, 440)

        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setViewportMargins(0, 20, 0, 20)
        self.setWidget(self.scrollWidget)
        self.setWidgetResizable(True)
        self.setObjectName('file_interface')

        self.scrollWidget.setObjectName("scrollWidget")
        
        StyleSheet.FILE_INTERFACE.apply(self)

        self.__initLayout()
        self.__connectSingalToSlot()

        self.info_view.resize(800, 420)

    def __initLayout(self):

        self.expandLayout.setSpacing(28)
        self.expandLayout.setContentsMargins(36, 10, 36, 0)
        
        self.filePathGroup.addSettingCard(self.sourceFolderCard)
        self.filePathGroup.addSettingCard(self.targetFolderCard)

        self.fileTidingGroup.addSettingCard(self.fileTidingCard)
        
        self.expandLayout.addWidget(self.filePathGroup)
        self.expandLayout.addWidget(self.fileTidingGroup)
        self.expandLayout.addWidget(self.info_view)

        self.info_view.hide()

    def __onSourceFolderCardClicked(self):
        path = cfg.get(cfg.sourceFolder)
        sourceFolder = QFileDialog.getExistingDirectory(self, '选择文件夹', path)
        if not sourceFolder or cfg.get(cfg.sourceFolder) == sourceFolder:
            return
        
        print('targetFolder:', sourceFolder)
        cfg.set(cfg.sourceFolder, sourceFolder)
        self.sourceFolderCard.setContent(sourceFolder)

    def __onTargetFolderCardClicked(self):
        path = cfg.get(cfg.targetFolder)
        targetFolder = QFileDialog.getExistingDirectory(self, '选择文件夹', path)
        if not targetFolder or cfg.get(cfg.targetFolder) == targetFolder:
            return
        
        print('targetFolder:', targetFolder)
        cfg.set(cfg.targetFolder, targetFolder)
        self.targetFolderCard.setContent(targetFolder)

    def __onFileTidingCardClicked(self): 
        self.info_view._init_info()
        self.info_view.hide()
        src_path = Path(cfg.get(cfg.sourceFolder))
        dst_path = Path(cfg.get(cfg.targetFolder))
        image_txt_pairs = []
        files_to_be_removed = []       
        file_paths = GetInformation.read_path(src_path)
        if len(file_paths) < 1:
            print('没有需要读取信息的zip文件')
            print("file_paths:", file_paths)
        else:
            for file_path in file_paths:
                jpg_path, txt_path, json_path = GetInformation.uncompress_zip(file_path, dst_path)
                # print('jpg_path:', jpg_path)
                # print('txt_path:', txt_path)
                # print('json_path:', json_path)
                image_txt_pairs.append((jpg_path, txt_path))
                # files_to_be_removed.append(jpg_path)
                files_to_be_removed.append(json_path)
            self.info_view.load_data(image_txt_pairs)
            self.info_view.show()

            for file in files_to_be_removed:
                file.unlink()

            print('image_txt_pairs:', image_txt_pairs)
            print('已将所有zip文件里需要读取的信息分别提取到txt文件中！')

    def __connectSingalToSlot(self):
        self.sourceFolderCard.clicked.connect(self.__onSourceFolderCardClicked)
        self.targetFolderCard.clicked.connect(self.__onTargetFolderCardClicked)        
        self.fileTidingCard.clicked.connect(self.__onFileTidingCardClicked)


